<?php
class configdb{
	var $server;
	var $username;
	var $password;
	var $database;
	
	function configdb(){
		$this->server	= "localhost";
		$this->username = "root";
		$this->password = "";
		$this->database = "richdoor";
	}
}
class db_init
{
	var $server;
	var $username;
	var $password;
	var $database;
	function db_init()
	{
		$config = new configdb();
      // Khai bao Server o day
		$this->server	= $config->server;
		$this->username = $config->username;
		$this->password = $config->password;
		$this->database = $config->database;
	}
	function log($filename, $content, $include_name = ""){
		
		$log_path = $_SERVER["DOCUMENT_ROOT"] . "/log/";
		$handle = @fopen($log_path . $filename . ".cfn", "a");
		//N?u handle chua có m? thêm ../
		if (!$handle) $handle = @fopen($log_path . $filename . ".cfn", "a");
		//N?u ko m? dc l?n 2 th exit luôn
		if (!$handle) exit();
		
		fwrite($handle, date("d/m/Y h:i:s A") . " " . @$_SERVER["REQUEST_URI"] . "\n" . $content . "\n");
		//fwrite($handle, date("d/m/Y h:i:s A") . "\n");
		fclose($handle); 	
			
	}	
	function __destruct()
	{
		unset($this->server);
		unset($this->username);
		unset($this->password);
		unset($this->database);
	}
}
?>
<?php
class db_query 
{
	var $result;
	var $links;
	var $time_slow_log = 0.5;
	
	function db_query($query,$file_include_name = "")
	{
	
		$dbinit = new db_init();
		//Khai bao connect
		$this->links = @mysql_connect($dbinit->server, $dbinit->username, $dbinit->password);
		
		if(!$this->links){
			echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
			echo '<meta name="revisit-after" content="1 days">';
			echo "<center>";
			echo "Chào bạn, trang web bạn yêu cầu hiện chưa thể thực hiện được. <br>";
			echo "Xin bạn vui lòng đợi vài giây rồi ấn <b>F5 để Refresh</b> lại trang web <br>";
			echo "</center>";
			exit();
		}
		
		$db_select = mysql_select_db($dbinit->database,$this->links);
		
		//echo $query;
		$time_start = $this->microtime_float();
		
		mysql_query("SET NAMES 'utf8'");
		$this->result = mysql_query($query,$this->links);
		
		$time_end = $this->microtime_float();
		$time = $time_end - $time_start;
		
		if ($time >= $this->time_slow_log){
			
			 $dbinit->log("sql_slow_log", $query, $file_include_name);
		}		
		if (!$this->result)
		{
			
			//ghi ra log loi query
			$error 			= "(" . mysql_errno($this->links) . ") " . mysql_error($this->links);
			$dbinit->log("sql_error_log", $query . " => " . $error, $file_include_name);
			mysql_close($this->links);
			die("Error in query string ");
		}
		unset($dbinit);
		
	}
	
	//trả về array
	function resultArray(){
		
		$arrayReturn = array();
		while($row = mysql_fetch_assoc($this->result)){
			$arrayReturn[] = $row;
		}
		
		return $arrayReturn;
	}
    
    //Kelvin edit
    //Ham tra ve ket qua duoi dang 1 hang du lieu, return Array
    function loadRow(){
        return mysql_fetch_assoc($this->result);
    }
    
    //Ham tra ve ket qua duoi dang 1 cot du lieu, return Array
    function loadColumn($column){
        $array = $this->resultArray();
        $result = array();
        foreach($array as $row)
            foreach($row as $key=>$value){
                if($key == $column){
                    $result[] = $value;
                }
            }
        return $result;
    }
    
    //Ham tra ve ket qua
    function loadResult(){
        $result = mysql_fetch_row($this->result);
        return $result[0];
    }
    
	function close()
	{
		mysql_free_result($this->result); 
		if ($this->links) 
		{  
			mysql_close($this->links);		
		}
	}
	//Hàm tính time
	function microtime_float()
	{
	   list($usec, $sec) = explode(" ", microtime());
	   return ((float)$usec + (float)$sec);
	}	
}
?>
<?php
class db_execute 
{
	var $links;
	var $total = 0;
	function db_execute($query, $utf8=1)
	{
		$dbinit = new db_init();
		
		$this->links = mysql_connect($dbinit->server, $dbinit->username, $dbinit->password);
		mysql_select_db($dbinit->database);
		
		unset($dbinit);
		mysql_query("SET NAMES 'utf8'");
		mysql_query($query);
		
		//kiểm tra thành công hay chưa
		$str = strtolower(substr(trim($query), 0, 6));
		$this->total = mysql_affected_rows();
		mysql_close($this->links);	
	}
}
class db_count{
	var $total;
	function db_count($sql){
		$db_ex = new db_query($sql);
		if($row = mysql_fetch_assoc($db_ex->result)){
			$this->total = intval($row["count"]);
		}else{
			$this->total = 0;
		}
		$db_ex->close();
		unset($db_ex);
		return $this->total;
	}
}
?>
<?php
class db_execute_return 
{
	var $links;
	var $result;
	var $total = 0;

	function db_execute($query)
	{
		$dbinit = new db_init();
		$this->links = mysql_connect($dbinit->server, $dbinit->username, $dbinit->password);
		mysql_select_db($dbinit->database);
		
		unset($dbinit);
		mysql_query("SET NAMES 'utf8'");
		mysql_query($query);
		$this->total = mysql_affected_rows();
		$last_id = 0;
		$this->result = mysql_query("select LAST_INSERT_ID() as last_id",$this->links);
		
		if($row=mysql_fetch_array($this->result)){
			$last_id = $row["last_id"];
		}
		
		mysql_close($this->links); 
		return $last_id;
	}
}
?>